# -*- coding: utf-8 -*-

'''eof
name:申请人（企业主）及申请企业已办理业务种类校验
code:AccomplishedBusiness_Check
tableName:
columnName:
groups:准入>企业业务信息校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import json
import pandas as pd
import datetime
from dateutil.relativedelta import relativedelta

reload(sys)
sys.setdefaultencoding('utf-8')
'''
ratingRequest 是行方返回的结果集
'''
def AccomplishedBusiness_Check():
    try:
        null_type_list = ['', None, 'null', '/', 'Null', '[]']
        check_list=['005','010001','010002','010003']
        refuse_list=["001", "001001", "001001001", "001001002", "001001003", "001002", "001002001", "001002001001", "001002001002", "001002002", "001002002001", "001003", "001003001", "001003001004", "001003002", "001003002001", "001003002002", "001003002003", "001003002004", "001003002005", "001003002006", "001003003", "001003003001", "001003003002", "001003003003", "001003003004", "001003003005", "001003003006", "001003004", "001003004001", "001004", "001004001", "001004001003", "001004002", "001004002003", "001004003", "001004003001", "001004004", "001004004001", "001005", "001005001", "001005001001", "001005001002", "001005001003", "001005002", "001005002001", "001005002002", "001005002003", "001005003", "001005003001", "001005003002", "001006", "001006001", "001006001001", "001007", "001007001", "001007001001", "001007002", "001007002001", "003", "003001", "003001001", "003001001001", "003001001002", "003001001003", "003002", "003002001", "003002001001", "003002002", "003002002001", "003002003", "003002003001", "003002004", "003002004001", "003003", "003003001", "003003001001", "003004", "003004001", "003004001001", "003004001002", "003004001003", "003004001004", "003004002", "003004002001", "003004002002", "003004002003", "003004003", "003004003001", "003004003002", "003004003003", "003004004", "003004004001", "003004004002", "003004005", "003004005001", "003004005002", "003004006", "003004006001", "003005", "003005001", "003005001001", "004", "004001", "004001001", "004001001001", "004001001002", "004001002", "004001002001", "004001002002", "004001003", "004001003001", "004001004", "004001004001", "004001005", "004001005001", "004001006", "004001006001", "004001006002", "004001007", "004001007001", "004002", "004002001", "004002001001", "004002002", "004002002001", "004006", "004006001", "004006001001", "004006001002", "004006001003", "060"]
        count = []
        #网贷返回的信用:005,保证:010
        personBankBusiTypeArr = ratingRequest['personBankBusiTypeArr']
        compBankBusiTypeArr = ratingRequest['compBankBusiTypeArr']
        if personBankBusiTypeArr in null_type_list and compBankBusiTypeArr in null_type_list:
            return u"缺失值"
        else:
            for i in json.loads(personBankBusiTypeArr):
                if i.has_key('mainWarrant') and i['mainWarrant'] not in null_type_list :
                    if str(i['mainWarrant']) in check_list or str(i[ 'mainWarrant'])  in refuse_list:
                        count.append(1)
                    else:
                        count.append(False)
                else:
                    count.append(None)
            for k in json.loads(compBankBusiTypeArr):
                if k.has_key('mainWarrant') and k[ 'mainWarrant'] not in null_type_list :
                    if str(k['mainWarrant']) in check_list or str(k['mainWarrant']) in refuse_list :
                        count.append(1)
                    else:
                        count.append(False)
                else:
                    count.append(None)
            countNotNone = filter(lambda x: x is not None, count)
            countNotFalse=filter(lambda  x:x is not False, countNotNone)
            if len(countNotNone) ==0:
                return u'缺失值'
            if len(countNotFalse)==0:
                return 0
            else:
                return sum(countNotNone)
    except:
        return u'缺失值'

result=AccomplishedBusiness_Check()

